const md5 = require("md5");
const {
    getUsers,
    addUser,
    deleteUser,
    updateUser,
    getUser,
} = require("../../models/user");

// 用户列表
const GetUsers = async (req, res) => {
    const data = await getUsers();
    res.send({
        code: 200,
        msg: "用户列表",
        data,
    });
};

// 用户详情
const GetUser = async (req, res) => {
    const data = await getUser(req.params.id);
    res.send({
        code: 200,
        msg: "用户详情",
        data,
    });
};
// 用户添加
const AddUser = async (req, res) => {
    const data = await addUser(req.body.username, md5(req.body.password));
    res.send({
        code: 200,
        msg: "用户添加成功",
        data,
    });
};

// 用户删除
const DeleteUser = async (req, res) => {
    const data = await deleteUser(req.params.id);
    res.send({
        code: 200,
        msg: "用户删除成功",
        data,
    });
};
// 用户更新
const UpdateUser = async (req, res) => {
    const { password, is_delete } = req.body;
    const update = {};
    if (password !== undefined) {
        update.password = md5(password);
    }
    if (is_delete !== undefined) {
        update.is_delete = is_delete;
    }
    console.log(update);
    const data = await updateUser(req.params.id, update);
    res.send({
        code: 200,
        msg: "用户更新成功",
        data,
    });
};

module.exports = { GetUsers, AddUser, DeleteUser, UpdateUser, GetUser };
